package com.ffcs.crmd.tsp.server.jdbc.service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.ffcs.crmd.tsp.common.exception.TspException;
import com.ffcs.crmd.tsp.server.jdbc.entity.Job;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobGroup;
import com.ffcs.crmd.tsp.server.jdbc.entity.TaskIdentity;
import com.ffcs.crmd.tsp.server.jdbc.repository.JobGroupRepository;
import com.ffcs.crmd.tsp.server.jdbc.service.IJobGroupService;
import com.ffcs.crmd.tsp.server.jdbc.service.IJobService;
import com.ffcs.crmd.tsp.server.jdbc.service.ITaskIdentityService;

/**
 * 
 * 功能说明:任务服务
 *
 * @author ZHONGFUHUA
 * 
 * @since 1.0.0-SNAPSHOT
 *
 */
public class JobGroupServiceImpl implements IJobGroupService {
    
    private IJobService          jobService;
    
    private ITaskIdentityService taskIdentityService;
    
    private JobGroupRepository   jobGroupRepository = new JobGroupRepository();;
    
    public JobGroupServiceImpl() {
        
    }
    
    @Override
    public JobGroup get(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobGroup.getId());
        params.add(jobGroup.getClusterName());
        params.add(jobGroup.getName());
        return jobGroupRepository.query(GET_JOB_GROUP, params, conn);
    }
    
    @Override
    public int querySize(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_JOB_GROUP, jobGroup, params, true);
        return jobGroupRepository.queryRows(sql, params, conn);
    }
    
    @Override
    public List<JobGroup> query(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtp(SELECT_JOB_GROUP, jobGroup, params, false);
        return jobGroupRepository.queryList(sql, params, conn);
    }
    
    @Override
    public List<JobGroup> queryPage(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        String sql = rtpPage(SELECT_JOB_GROUP, jobGroup, params);
        return jobGroupRepository.queryList(sql, params, conn);
    }
    
    @Override
    public boolean add(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(TspSeqServiceImpl.getSeq());
        params.add(jobGroup.getName());
        params.add(jobGroup.getDescription());
        params.add(jobGroup.getClusterName());
        return jobGroupRepository.execute(INSERT_JOB_GROUP, params, conn);
    }
    
    @Override
    public boolean update(Connection conn, JobGroup jobGroup) throws TspException {
        List<Object> params = new ArrayList<Object>();
        params.add(jobGroup.getClusterName());
        params.add(jobGroup.getName());
        params.add(jobGroup.getDescription());
        params.add(jobGroup.getId());
        return jobGroupRepository.execute(UPDATE_JOB_GROUP, params, conn);
    }
    
    @Override
    public boolean delete(Connection conn, JobGroup jobGroup) throws TspException {
        validate(jobGroup);
        List<Object> params = new ArrayList<Object>();
        params.add(jobGroup.getId());
        
        Job job = new Job();
        job.setJobGroupId(jobGroup.getId());;
        jobService.delete(conn, job);
        
        TaskIdentity taskIdentity = new TaskIdentity();
        taskIdentity.setJobGroupId(jobGroup.getId());
        taskIdentityService.delete(conn, taskIdentity);
        return jobGroupRepository.execute(DELETE_JOB_GROUP, params, conn);
    }

    /**
     * 功能说明: 设置jobService
     *
     * @param jobService jobService 
     */
    @Override
    public void setJobService(IJobService jobService) {
        this.jobService = jobService;
    }
    

    /**
     * 功能说明: 设置taskIdentityService
     *
     * @param taskIdentityService taskIdentityService 
     */
    @Override
    public void setTaskIdentityService(ITaskIdentityService taskIdentityService) {
        this.taskIdentityService = taskIdentityService;
    }
    
    
    /**
     * 
     * 功能说明:
     * 
     * @param jobGroup
     * @throws TspException
     */
    private void validate(JobGroup jobGroup) throws TspException {
        if (jobGroup.getId() == null) {
            throw new TspException("['id'] can't be null");
        }
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobGroup
     * @param params
     * @return
     */
    private String rtp(String sql, JobGroup jobGroup, final List<Object> params, boolean isLike) {
        if (StringUtils.isNoneBlank(jobGroup.getName())) {
            if (isLike) {
                sql += " AND TJG." + COL_NAME + " LIKE ?";
                params.add("%" + jobGroup.getName() + "%");
            }else{
                sql += " AND TJG." + COL_NAME + " = ?";
                params.add(jobGroup.getName());
            }
        }
        
        if (StringUtils.isNoneBlank(jobGroup.getClusterName())) {
            sql += " AND TJG." + COL_CLUSTER_NAME + " = ?";
            params.add(jobGroup.getClusterName());
        }
        
        if(StringUtils.isNoneBlank(jobGroup.getIdentity())){
            sql += " AND TTI." + COL_TASK_IDENTITY_IDENTITY + " LIKE ?";
            params.add("%" + jobGroup.getIdentity() + "%");
        }
        return sql;
    }
    
    /**
     * 
     * 功能说明:
     * 
     * @param sql
     * @param jobGroup
     * @param params
     * @return
     */
    private String rtpPage(String sql, JobGroup jobGroup, final List<Object> params) {
        String rsql = rtp(sql, jobGroup, params, false);
        rsql += " LIMIT ?,?";
        params.add(jobGroup.getPageNumber());
        params.add(jobGroup.getPageSize());
        return rsql;
    }
}
